<template>
    <div class="user_member">
        <wrapper-content pageTitle="公告管理">
            <div class="data-content">
                <div class="table-edit">
                    <div class="left-actions">
                        <Button permission="" type="primary" shape="circle" icon="plus" @click="add">
                            新增
                        </Button>
                    </div>
                </div>
                <Table :loading="loading" border :columns="columns" :data="user_list" class="no-border-table"
                       @on-selection-change="selectItem"></Table>
                <Page v-if="userCount > 0" :total="userCount" :current="page_num" @on-change="changePage"
                      @on-page-size-change="changePageSize" size="small" show-total show-sizer class="table-page">
                    <slot class="total">共 {{ userCount }} 条</slot>
                </Page>
            </div>
        </wrapper-content>

        <!--删除-->
        <Modal
                v-model="del_model"
                title="操作提示">
            <p>真的要删除当前选中项吗？一旦删除将无法恢复，请想好了再决定 </p>
            <div slot="footer">
                <Button type="text" @click="del_model = false">取消</Button>
                <Button type="error" :loading="send_loading" @click="delConfirm">删了</Button>
            </div>
        </Modal>

        <!--修改-->
        <Modal
                v-model="form_modal"
                width="360"
                class="form-modal">
            <p slot="header" style="text-align:center;">
                <span>{{ form_title }}</span>
            </p>
            <Form ref="formValidate" :model="formValidate"
                  label-position="left">
                <Form-item prop="time" label="休市日期：" style="margin-bottom: 0">
                    <DatePicker :value="formValidate.time" @on-change="changeEndDate" :editable="false" type="date" style="width: 100%"></DatePicker>
                </Form-item>
                <Form-item prop="text" label="休市说明：" style="margin-bottom: 0">
                    <Input v-model="formValidate.text" type="text"/>
                </Form-item>

                <div class="footer-item text-center pt10">
                    <Form-item>
                        <Button type="primary" size="large" @click="handleSubmit('formValidate')"
                                :loading="send_loading" long>{{ form_submit }}
                        </Button>
                    </Form-item>
                </div>
            </Form>
            <div slot="footer">

            </div>
        </Modal>

    </div>
</template>

<script>
    import WrapperContent from '../../../components/wrapper-content.vue'
    import {getUserList, getUser, getLevelList, getPositionList,doUserInfo,delUser,changeUserState} from "@/api/user";
    import * as utils from '../../../assets/js/utils'
    import $ from 'jquery'
    import _ from 'lodash'

    export default {
        components: {
            WrapperContent,
        },
        data() {
            return {
                self: this,
                form_modal: false,
                form_action: 'add',
                form_title: '添加',
                form_url: 'add',
                form_submit: '添加',
                formValidate: {
                    time:'',
                    text:''
                },
                level_list: [],
                position_list: [],
                del_model: false,
                select_users: [],
                send_loading: false,
                page_size: 10,
                page_num: 1,
                keyword: '',
                loading: true,
                columns: [
                    {
                        title: '编号',
                        key: 'account',
                        width:60,
                    },
                    {
                        title: '休市日期',
                        key: 'realname',
                        width:100,
                    },
                    {
                        title: '说明',
                        key: 'realname',
                    },
                    {
                        title: '设置',
                        key: 'action',
                        align:'center',
                        width:120,
                        render: (h, params) => {
                            return h('div', [
                                h('Button', {
                                    props: {
                                        type: 'text',
                                        size: 'small'
                                    },
                                    on: {
                                        click: () => {
                                            event.stopPropagation(); //阻止冒泡
                                            this.add('edit');
                                            // this.$router.push({path:'/content/add/1'})
                                        }
                                    }
                                }, '修改'),
                                h('Button', {
                                    props: {
                                        type: 'text',
                                        size: 'small'
                                    },
                                    on: {
                                        click: () => {
                                            event.stopPropagation(); //阻止冒泡
                                            this.del_model = true ;
                                        }
                                    }
                                }, '删除'),
                            ]);
                        }
                    }
                ],
                user_list: [{title:'sjdhgfjsdgfjsdgfyurgawkebgjksdbvjh'}],
                userCount: 0,
            }
        },
        watch: {
            form_modal: function (value) {
                if (value === false) {
                    this.formValidate = {
                        user_id: 0,
                        account: '',
                        realname: '',
                        password: '',
                        password_confirm: '',
                        state: 1,
                    }
                }
            },
            keyword: function (newQuestion) {
                this.search()
            },
            '$route'(to, from) { // 路由监听，重新获取数据
                if (this.$store.state.list_reload) {
                    this.getUserList()
                }
            }
        },
        created: function () {
            this.loading = false ;
            this.userCount = 10;
            /*this.getUserList();
            this.getLevelList();
            this.getPositionList()*/
        },
        methods: {
            getUserList() {
                let app = this;
                getUserList(this.page_size, this.page_num, this.keyword).then(res => {
                    app.loading = false;
                    app.user_list = res.data.list;
                    app.userCount = Number(res.data.count)
                });
            },
            getInfo() {
                let app = this;
                getUser(app.formValidate.user_id).then(res => {
                    if (res.data) {
                        app.formValidate.user_id = res.data.id;
                        app.formValidate.account = res.data.account;
                        app.formValidate.realname = res.data.realname;
                        app.formValidate.level_id = res.data.level_id;
                        app.formValidate.position_id = res.data.position_id;
                        app.formValidate.state = res.data.state
                    }
                });
            },
            getLevelList() {
                let app = this;
                getLevelList(50).then(res => {
                    app.level_list = res.data.list
                });
            },
            getPositionList() {
                let app = this;
                getPositionList(50).then(res => {
                    app.position_list = res.data.list
                });
            },
            selectItem(selection) {
                let app = this;
                app.select_teams = [];
                $.each(selection, function (k, v) {
                    app.select_teams.push(v.id)
                });
            },
            handleSubmit(name) {
                this.send_loading = true;

                setTimeout(function () {
                    this.send_loading = false;
                    this.form_modal = false;
                },1000);
            },
            delConfirm() {
                console.log('删除！');
                // this.delItem()
            },
            delItem() {
                let app = this;
                app.send_loading = true;
                delUser(JSON.stringify(app.select_users)).then(res => {
                    app.send_loading = false;
                    app.del_model = false;
                    if (res.ret === 200) {
                        app.$Message.success('删除成功');
                        app.getUserList()
                    } else {
                        app.$Message.warning(res.msg);
                    }
                });
            },
            changePage(page) {
                this.page_num = page;
                this.getUserList()
            },
            changePageSize(page_size) {
                this.page_num = 1;
                this.page_size = page_size;
                this.getUserList()
            },
            reloadList() {
                this.getUserList(1, this.page_size)
            },
            rowClassName(row, index) {
                return 'rowClassName';
            },
            changeState(id, state, index) {
                let app = this;
                const change_state = state == 1 ? 0 : 1;
                changeUserState(id,state == 1 ? 0 : 1).then(res => {
                    app.user_list[index].state = change_state
                });
            },
            changeConfirm(){//确认修改

            },
            add(type){//添加新会员
                if(type == 'edit'){
                    this.form_title = '编辑';
                    this.form_submit = '保存';
                    this.form_url = 'edit';
                /*    this.formValidate.user_id = id;
                    this.getInfo();
                    this.form_modal = true;
                    this.form_action = 'edit'*/
                }else{
                    this.form_title = '添加';
                    this.form_submit = '添加';
                    this.form_url = 'add';
                }
                this.form_modal = true ;
            },
            changeEndDate(date) {
                this.formValidate.time = date;
            },
        }
    }
</script>













